package com.eschool.service.impl;

import java.util.List;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import com.eschool.model.User;
import com.eschool.service.AlertService;
import com.eschool.util.UserUtil;

public class AlertServiceImpl implements AlertService{

	@Override
	public boolean sendAlert(String subject, String mes, List<User> reciepients, String attachment) {
		
		String from = "eschool.cloud@gmail.com";
		   
		final String username = "eschool.cloud";
		   
		final String password = "eschool@cloud";
		
		final String header = "Dear User,";
		
		final String thanx = "Thanks,";

		String school="";
		
		String sentBy = ""; 
		
		if(UserUtil.currentUser() != null){
			sentBy = UserUtil.currentUser().getName() + ",\n" + 
									UserUtil.currentUser().getRole().getRoleName();
			school = UserUtil.currentUser().getBranch().getSchool().getSchoolName() + 
					" - " + UserUtil.currentUser().getBranch().getBranchTitle();
		}
		else if(UserUtil.currentAdmin() != null)
			sentBy = UserUtil.currentAdmin().getName() + ",\n" + 
					UserUtil.currentAdmin().getRole().getRoleName();
		
		
		
		mes = header + "\n\n" + mes + "\n\n" + thanx + ",\n" + sentBy + ",\n" + school;
		
		Properties props = new Properties();
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.starttls.enable", "true");
		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.port", "587");

		Session session = Session.getInstance(props,
					  new javax.mail.Authenticator() {
						protected PasswordAuthentication getPasswordAuthentication() {
							return new PasswordAuthentication(username, password);
						}
					  });
		
	      InternetAddress[] addrList = new InternetAddress[reciepients.size()];
	      try{
	    	  
	    	  
	    	  for(int i=0; i<reciepients.size(); i++)
	    	  {
	    		  
	    		  addrList[i] =  new InternetAddress( (String)((User)reciepients.get(i)).getEmail() );
	    		  System.out.println(i+ "    " + addrList[i]);
	    	  }
		      // Create a default MimeMessage object.
		      MimeMessage message = new MimeMessage(session);

		      // Set From: header field of the header.
		      message.setFrom(new InternetAddress(from));
		      
		      message.setRecipients(Message.RecipientType.TO, addrList);
		      message.setSubject(subject);

		      // Create the message part 
		      BodyPart messageBodyPart = new MimeBodyPart();
		      messageBodyPart.setText(mes);
		      
		      Multipart multipart = new MimeMultipart();

		      // Set text message part
		      multipart.addBodyPart(messageBodyPart);

		      // Part two is attachment
		      if(attachment != null)
		      
		      {
		    	  messageBodyPart = new MimeBodyPart();
		    	  String filename = attachment;
			      DataSource source = new FileDataSource(filename);
			      messageBodyPart.setDataHandler(new DataHandler(source));
			      messageBodyPart.setFileName("Resume.doc");
			      multipart.addBodyPart(messageBodyPart);
		      }

		      // Send the complete message parts
		      message.setContent(multipart );

		      // Send message
		      Transport.send(message);
		      
		      System.out.println("Sent");
		      
//		      User sender = UserUtil.currentUser();
//		      
//		      addAlert(subject, mes, sender, reciepients, attachment);
		      
		      return true;
	      }
		   catch (MessagingException mex) {
			   mex.printStackTrace();
			   return false;
		   }
	}
}
